#pragma once

#include <iostream>
#include <string>
#include "Protocol.hpp"

class Servercal
{
public:
    Servercal()
    {
    }

    Response Calculator_function(Request &res)
    {
        Response ret(0, 0);
        switch (res.op)
        {
        case '+': ret.result = res.x + res.y;
            break;
            case '-': ret.result = res.x - res.y;
            break;
            case '*': ret.result = res.x * res.y;
            break;
            case '/': {
                if (res.y == 0) {
                    ret.Flags = 1;
                } else ret.result = res.x / res.y;
            } break;
            case '%': {
                if (res.y == 0) {
                    ret.Flags = 2;
                } else ret.result = res.x % res.y;
            } break;
            default:
            ret.Flags = 3;
            break;
        }
        return ret;
    }

    std::string Calculator(std::string &out) // 传来网络报文
    {
        std::string temp ;
        bool flag =Decode(out,&temp);
        if (!flag)
            return "";
        Request res;
        int ret = res.Deserialization(temp);

        if (!ret)
            return "";

        temp = "";
        Response answer = Calculator_function(res);
        answer.Serialization(&temp);
        temp = Encode(temp);

        return temp;
    }
    ~Servercal()
    {
    }
};